var HorizontalRules = function(){
	var _self = this;
	this.calculateValue = function(row, dice){
		var ret = {
			value : 0,
			status : true
		}
		switch(row){
			case ONE.name : {
				ret.value = _self.getSumByValue(1, dice);
				break;
			}
			case TWO.name : {
				ret.value = _self.getSumByValue(2, dice);
				break;
			}
			case THREE.name : {
				ret.value = _self.getSumByValue(3, dice);
				break;
			}
			case FOUR.name : {
				ret.value = _self.getSumByValue(4, dice);
				break;
			}
			case FIVE.name : {
				ret.value = _self.getSumByValue(5, dice);
				break;
			}
			case SIX.name : {
				ret.value = _self.getSumByValue(6, dice);
				break;
			}
			case MAX.name : {
				ret.value = _self.getSum(dice);
				if(dice.length !== 5){
					ret.status = false;
				}
				break;
			}
			case MIN.name : {
				ret.value = _self.getSum(dice);
				if(dice.length !== 5){
					ret.status = false;
				}
				break;
			}
			case KENTA.name : {
				if(dice.length !== 5){
					ret.status = false;
				}
				else{
					if(_self.checkKenta(dice)){
						ret.value = (_self.getSum(dice) + BONUSES[KENTA.name]);
					}
					else{
						ret.value = 0;
					}
				}
				break;
			}
			case TRILLING.name : {
				if(dice.length !== 3){
					ret.status = false;
				}
				else{
					if(_self.checkTrilling(dice)){
						ret.value = (_self.getSum(dice) + BONUSES[TRILLING.name]);
					}
					else{
						ret.value = 0;
					}
				}
				break;
			}
			case FULL.name : {
				if(dice.length !== 5){
					ret.status = false;
				}
				else{
					if(_self.checkFull(dice)){
						ret.value = (_self.getSum(dice) + BONUSES[FULL.name]);
					}
					else{
						ret.value = 0;
					}
				}
				break;
			}
			case POKER.name : {
				if(dice.length !== 4){
					ret.status = false;
				}
				else{
					if(_self.checkPoker(dice)){
						ret.value = (_self.getSum(dice) + BONUSES[POKER.name]);
					}
					else{
						ret.value = 0;
					}
				}
				break;
			}
			case YAMB.name : {
				if(dice.length !== 5){
					ret.status = false;
				}
				else{
					if(_self.checkYamb(dice)){
						ret.value = (_self.getSum(dice) + BONUSES[YAMB.name]);
					}
					else{
						ret.value = 0;
					}
				}
				break;
			}
		}
		return ret;
	};
	this.getSumByValue = function(value, dice){
		var sum = 0;
		for(var i=0; i<dice.length; i++){
			if(dice[i] === value){
				sum += dice[i];
			}
		}
		return sum;
	}
	this.getSum = function(dice){
		var sum = 0;
		for(var i=0; i<dice.length; i++){
			sum += dice[i];
		}
		return sum;
	}
	this.checkTrilling = function(dice){
		if(dice.length != 3) {
			return false;	
		}
		else{
			if(dice[0] === dice[1] && dice[1] === dice[2]){
				return true;
			}
			else{
				return false;
			}
		}
	}
	this.checkPoker = function(dice){
		if(dice.length != 4) {
			return false;	
		}
		else{
			if(dice[0] === dice[1] && dice[1] === dice[2] && dice[2] === dice[3]){
				return true;
			}
			else{
				return false;
			}
		}
	}
	this.checkYamb = function(dice){
		if(dice.length != 5) {
			return false;	
		}
		else{
			if(dice[0] === dice[1] && dice[1] === dice[2] && dice[2] === dice[3] && dice[3] === dice[4]){
				return true;
			}
			else{
				return false;
			}
		}
	}
	this.checkKenta = function(dice){
		if(dice.length != 5) {
			return false;
		}
		else{
			dice = dice.sort();
			var flagSum = 0;
			for(var i=0; i<dice.length-1; i++){
				if(dice[i+1] - dice[i] === 1){
					flagSum += 1;
				}else{
					break;
				}
			}
			if(flagSum === dice.length - 1) {
				return true;
			}
			else {
				return false;
			}
		}
		
	};
	this.checkFull = function(dice){
		if(dice.length != 5) {
			return false;
		}
		else{
			dice = dice.sort();
			if((dice[0] === dice[1]) && (dice[1] === dice[2]) && (dice[3] === dice[4]) && (dice[0] !== dice[4])){
				return true;
			}
			else if((dice[0] === dice[1]) && (dice[2] === dice[3]) && (dice[3] === dice[4]) && (dice[0] !== dice[4])){
				return true;
			}
			else{
				return false;
			}	
		}
		
	};
};